from info import create_app, db, models
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from flask import current_app, jsonify
from info.utils.response_code import RET


app = create_app('development')

manage = Manager(app)


# 创建函数，以添加管理员
@manage.option('-n', '--name', dest='name')
@manage.option('-p', '--password', dest='password')
def create_admin(name, password):
    user = models.User()
    user.mobile = name
    user.nick_name = name
    user.password = password
    user.is_admin = True
    try:
        db.session.add(user)
        db.session.commit()
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='管理员添加失败')
    return '添加成功'


# database migrate
Migrate(app, db)
manage.add_command('db', MigrateCommand)


if __name__ == '__main__':
    manage.run()
